package 自定义异常类;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-06-21
 * Time: 20:13
 */
public class Login1 {
    private String userName;
    private String password;

    public Login1(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void loginInfo(String userName, String password) throws UserNameException, passwordException {
        if(!this.userName.equals(userName)) {
            //抛出异常：
            throw new UserNameException("用户名错误！");
        }
        if(!this.password.equals(password)) {
            //抛出异常：
            throw new passwordException("密码错误！");
        }
        System.out.println("登陆成功！");
    }

    public static void main(String[] args) {
        try {
            Login1 login1 = new Login1("reborn", "11223344");
            login1.loginInfo("张三", "11223344");
        } catch (UserNameException e) {
            e.printStackTrace();
        } catch (passwordException e) {
            e.printStackTrace();
        }
    }
}

/*
注意事项
- 自定义异常通常会继承自Exception或者RuntimeException
- 继承自Exception的异常默认是受查异常
- 继承自RuntimeException的异常默认是非受查异常
 */